package com.hyacinth.lotterysystem.controller;

import com.hyacinth.lotterysystem.common.pojo.CommonResult;
import com.hyacinth.lotterysystem.common.utils.JacksonUtil;
import com.hyacinth.lotterysystem.dto.req.UserEmailLoginReq;
import com.hyacinth.lotterysystem.dto.req.UserPasswordLoginReq;
import com.hyacinth.lotterysystem.dto.req.UserRegisterReq;
import com.hyacinth.lotterysystem.dto.resp.UserInfoListResp;
import com.hyacinth.lotterysystem.dto.resp.UserLoginResp;
import com.hyacinth.lotterysystem.dto.resp.UserRegisterResp;
import com.hyacinth.lotterysystem.service.UserService;
import com.hyacinth.lotterysystem.service.enums.UserIdentityEnum;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public CommonResult<UserRegisterResp> userRegister(@Validated @RequestBody UserRegisterReq param) {
        logger.info("用户注册 -> UserRegisterReq: {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(userService.register(param));
    }

    @PostMapping("/password/login")
    public CommonResult<UserLoginResp> userPasswordLogin(@RequestBody UserPasswordLoginReq param){
        logger.info("用户密码登录 -> UserPasswordLoginReq: {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(userService.login(param));
    }

    @PostMapping("/email/login")
    public CommonResult<UserLoginResp> userEmailLogin(@RequestBody UserEmailLoginReq param){
        logger.info("用户邮箱登录 -> UserEmailLoginReq: {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(userService.login(param));
    }

    @GetMapping("/list")
    public CommonResult<List<UserInfoListResp>> selectUserListByIdentity(String identity){
        logger.info("查询用户列表 -> identity: {}", identity);
        return CommonResult.success(userService.selectUserListByIdentity(UserIdentityEnum.forName(identity)));
    }
}
